package com.itfenghuang.day15;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/*
文件上传的服务端
 */
public class A04_UnloadServer {
    public static void main(String[] args) throws IOException {
//        创建ServerSocket
        ServerSocket ss = new ServerSocket(12333);
        while (true){
            //        监听连接
            Socket at = ss.accept();
        new Thread(new Runnable() {
            @Override
            public void run() {
            try{
                //        获取网络输入流
                InputStream is = at.getInputStream();
//        创建本地输出流对象
//        文件名   img+当前系统时间的毫秒值+随机数(0-9)+".jpg"
                String fileName="img"+System.currentTimeMillis()+new Random().nextInt(10)+".jpg";
                FileOutputStream fos = new FileOutputStream("F:\\aaa\\"+fileName);
//        循环读写
                byte[] bys = new byte[1024];
                int len;
                System.out.println("4444");
                while ((len=is.read(bys))!=-1){
                    fos.write(bys,0,len);
                }
                System.out.println("5555");
//        回送消息
                OutputStream os = at.getOutputStream();
                os.write("上传成功".getBytes());
//        释放资源
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            }
        }).start();

        }
    }
}
